<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-input
            v-model="keyValue"
            placeholder="请输入储罐名称"
            clearable
            prefix-icon="Search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="tankOptions"
            :props="{ label: 'tankName', child: '' }"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tankRef"
            node-key="tankId"
            highlight-current
            default-expand-all
            @node-click="handleNodeClick"
          >
            <template class="custom-tree-node" #default="{ node, data }">
              <div
                style="
                  display: flex;
                  align-items: flex-start;
                  justify-content: space-between;
                  width: 100%;
                "
              >
                <span style="padding-left: 4px"
                  >{{ node.label }}--({{ data.deptName }})</span
                >
                <span>
                  <el-button
                    link
                    type="primary"
                    icon="Edit"
                    @click="handleTankUpdate(data)"
                    v-hasPermi="['risk:target:edit']"
                  ></el-button>
                  <el-button
                    link
                    type="primary"
                    icon="Delete"
                    @click="handleTankDelete(data)"
                    v-hasPermi="['risk:target:remove']"
                  ></el-button>
                </span>
              </div>
            </template>
          </el-tree>
        </div>
        <div class="head-container" style="margin-top: 15px; text-align: center">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleTankAdd"
            v-hasPermi="['system:user:add']"
            >新增储罐</el-button
          >
        </div>
      </el-col>
      <el-col :span="18" :xs="24">
        <!--操作栏-->
        <el-row :gutter="10" class="mb8">
          <el-col :span="20"></el-col>
          <el-col :span="4">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              >新增监测位号</el-button
            >
          </el-col>
        </el-row>
        <!--数据列表-->
        <el-table v-loading="loading" :data="dataList">
          <el-table-column type="index" label="序号" width="55" align="center" />
          <el-table-column label="所属储罐设备" prop="tankId">
            <template #default="scope">
              <span>{{ scope.row.tankId ? parseName(scope.row.tankId) : "---" }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="监测点名称"
            prop="monitoringName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="类型名称" width="100" prop="parameterName" />
          <el-table-column label="第一报警值" width="120" prop="firstStageAlarm" />
          <el-table-column label="第二报警值" width="120" prop="secondStageAlarm" />
          <el-table-column label="参数单位" width="100" prop="units" />
          <el-table-column
            label="位号"
            prop="bitNumber"
            width="260"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-tooltip content="编辑" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.current"
          v-model:limit="queryParams.size"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!--新增或者修改储罐设备对话框-->
    <el-dialog :title="title" v-model="openTank" width="60%" append-to-body>
      <el-form ref="tankFormRef" :model="tankForm" :rules="tankRules">
        <el-form-item label="储罐类型:" label-width="140px" prop="tankType">
          <el-select v-model="tankForm.tankType" placeholder="请选择储罐类型">
            <el-option
              v-for="dict in tankTypeOptions"
              :key="dict.id"
              :label="dict.label"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="储罐编号:" label-width="140px" prop="tankCode">
          <el-input v-model="tankForm.tankCode" placeholder="请输入储罐编号" />
        </el-form-item>
        <el-form-item label="储罐名称:" label-width="140px" prop="tankName">
          <el-input v-model="tankForm.tankName" placeholder="请输入储罐名称" />
        </el-form-item>
        <el-form-item label="所在部门:" label-width="140px" prop="deptId">
          <el-tree-select
            v-model="tankForm.deptId"
            :data="deptOptions"
            :props="{ value: 'id', label: 'label', children: 'children' }"
            value-key="id"
            placeholder="请选择部门"
            check-strictly
          />
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="所在位置经度:" label-width="140px" prop="latitude">
              <el-input
                v-model="tankForm.latitude"
                placeholder="请输入储罐所在位置经度"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在位置纬度:" label-width="140px" prop="longitude">
              <el-input
                v-model="tankForm.longitude"
                placeholder="请输入储罐所在位置纬度"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="储罐所在坐标:" label-width="140px" prop="coord">
          <el-input v-model="tankForm.coord" placeholder="请输入储罐所在坐标" />
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="8">
            <el-form-item label="储罐直径:" label-width="140px" prop="tankDiameter">
              <el-input v-model="tankForm.tankDiameter" placeholder="请输入储罐直径" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储罐厚度:" label-width="140px" prop="tankThickness">
              <el-input v-model="tankForm.tankThickness" placeholder="请输入储罐厚度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储罐高度:" label-width="140px" prop="tankHeight">
              <el-input v-model="tankForm.tankHeight" placeholder="请输入储罐高度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item
          label="储罐存放材料:"
          label-width="140px"
          prop="tankStoredSubstance"
        >
          <el-input
            v-model="tankForm.tankStoredSubstance"
            placeholder="请输入储罐存放材料"
          />
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="使用年限:" label-width="140px" prop="tankDesignLife">
              <el-input
                v-model="tankForm.tankDesignLife"
                placeholder="请输入储罐使用年限"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建成日期:" label-width="140px" prop="tankConstructTime">
              <el-date-picker
                v-model="tankForm.tankConstructTime"
                value-format="YYYY-MM-DD"
                type="date"
                placeholder="请选择储罐建成日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="备案编号:" label-width="140px" prop="tankRecordsCode">
              <el-input
                v-model="tankForm.tankRecordsCode"
                placeholder="请输入储罐备案编号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备案日期:" label-width="140px" prop="tankRecordsTime">
              <el-date-picker
                v-model="tankForm.tankRecordsTime"
                value-format="YYYY-MM-DD"
                type="date"
                placeholder="请选择储罐备案日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitTankForm">确 定</el-button>
          <el-button @click="cancelTank">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!--新增或者修改储罐设备监测点对话框-->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item label="所属储罐设备:" label-width="140px" prop="tankId">
          <el-select v-model="form.tankId" placeholder="请选择所属储罐设备">
            <el-option
              v-for="dict in tankOptions"
              :key="dict.tankId"
              :label="dict.tankName"
              :value="dict.tankId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="监测点名称:" label-width="140px" prop="monitoringName">
          <el-input v-model="form.monitoringName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="监测点位号:" label-width="140px" prop="bitNumber">
          <el-input v-model="form.bitNumber" placeholder="请输入位号" />
        </el-form-item>
        <el-form-item label="监测参数类型:" label-width="140px" prop="parameterType">
          <el-select v-model="form.parameterType" placeholder="请选择监测参数类型">
            <el-option
              v-for="dict in paramTypeptions"
              :key="dict.id"
              :label="dict.label"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="监测参数单位:" label-width="140px" prop="units">
          <el-input v-model="form.units" placeholder="请输入参数单位" />
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="第一报警值:" label-width="140px" prop="firstStageAlarm">
              <el-input v-model="form.firstStageAlarm" placeholder="请输入第一报警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第二报警值:" label-width="140px" prop="secondStageAlarm">
              <el-input v-model="form.secondStageAlarm" placeholder="请输入第二报警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="参数报警规则:" label-width="140px" prop="alarmType">
          <el-select v-model="form.alarmType" placeholder="请选择所属监测点报警规则">
            <el-option
              v-for="dict in rulesOptions"
              :key="dict.code"
              :label="dict.value"
              :value="dict.code"
            />
          </el-select>
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="量程最小值:" label-width="140px" prop="minimumRange">
              <el-input v-model="form.minimumRange" placeholder="请输入量程最小值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="量程最大值:" label-width="140px" prop="maximumRange">
              <el-input v-model="form.maximumRange" placeholder="请输入量程最大值" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="ScreenTank">
import {
  listTank,
  addTank,
  deleteTank,
  updateTank,
  getTank,
  listMonitor,
  addMonitor,
  deleteMonitor,
  updateMonitor,
  getMonitor,
  getMonitorRule,
} from "@/api/screen/tank";
import { deptTreeSelect } from "@/api/system/user";
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const title = ref("");
const open = ref(false);
const openTank = ref(false);
const keyValue = ref(""); //通过关键字搜索
const checkTank = ref(undefined);
const { proxy } = getCurrentInstance();
const deptOptions = ref([]);
const tankOptions = ref([
  { id: 1, label: "储罐A" },
  { id: 2, label: "储罐B" },
]);
const tankTypeOptions = ref([
  { id: "1", label: "垂直罐" },
  { id: "2", label: "圆柱形罐" },
  { id: "3", label: "矩形罐" },
  { id: "4", label: "球形罐" },
  { id: "5", label: "膜式罐" },
  { id: "6", label: "卧式罐" },
]);
const paramTypeptions = ref([
  { id: "1", label: "温度" },
  { id: "2", label: "液位" },
  { id: "3", label: "压力" },
]);
const rulesOptions = ref([]);
const data = reactive({
  form: {},
  tankForm: {},
  queryParams: {
    current: 1,
    size: 10,
  },
  rules: {
    tankId: [
      { required: true, message: "所属储罐设备必须选择！", trigger: ["click", "change"] },
    ],

    monitoringName: [
      { required: true, message: "监测点名称必须填写！", trigger: "blur" },
    ],
    bitNumber: [{ required: true, message: "监测点位号必须填写！", trigger: "blur" }],
    parameterType: [
      { required: true, message: "监测点参数类型必须选择！", trigger: "blur" },
    ],
    firstStageAlarm: [
      { required: true, message: "第一报警值必须填写！", trigger: "blur" },
    ],
    secondStageAlarm: [
      { required: true, message: "第二报警值必须填写！", trigger: "blur" },
    ],
    alarmType: [{ required: true, message: "报警规则必须选择", trigger: "blur" }],
  },
  tankRules: {
    tankName: [
      { required: true, message: "储罐名称必须填写！", trigger: ["blur", "change"] },
      {
        min: 3,
        max: 100,
        message: "名称必须介于 3 和 50 之间",
        trigger: "blur",
      },
    ],
    tankCode: [
      { required: true, message: "储罐编号必须填写！", trigger: ["blur", "change"] },
      {
        min: 3,
        max: 100,
        message: "编号必须介于 3 和 50 之间",
        trigger: "blur",
      },
    ],
    tankType: [{ required: true, message: "储罐类型必须选择！", trigger: "change" }],
    deptId: [{ required: true, message: "所属部门必须选择！", trigger: "change" }],
    coord: [{ required: true, message: "三维坐标必须填写！", trigger: "blur" }],
    latitude: [{ required: true, message: "经度必须填写！", trigger: "blur" }],
    longitude: [{ required: true, message: "纬度必须填写！", trigger: "blur" }],
  },
});
const { queryParams, form, tankForm, rules, tankRules } = toRefs(data);

/** 解析储罐设备名称 */
function parseName(val) {
  var itemTarget = tankOptions.value.filter((item) => {
    return item.tankId == parseInt(val);
  });
  return itemTarget[0].tankName;
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

/** 获取储罐设备数据 */
function getTankList() {
  loading.value = true;
  listTank().then((response) => {
    loading.value = false;
    tankOptions.value = response.data;
  });
}
/** 获取数据 */
function getList() {
  loading.value = true;
  listMonitor(queryParams.value).then((response) => {
    loading.value = false;
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
  });
}
/** 根据名称筛选风险分析对象 */
watch(keyValue, (val) => {
  proxy.$refs["tankRef"].filter(val);
});
/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  if (!value) return true;
  return data.tankName.indexOf(value) !== -1;
};
/** 节点单击事件 */
function handleNodeClick(data) {
  //console.log(data);
  checkTank.value = data.tankId;
  queryParams.value.tankId = data.tankId;
  getList();
}

/** 重置储罐表单 */
function resetTank() {
  tankForm.value = {};
  proxy.resetForm("tankFormRef");
}

/** 新增储罐设备 */
function handleTankAdd() {
  openTank.value = true;
  title.value = "新增储罐设备";
  resetTank();
}

/** 新增储罐设备 */
function handleTankUpdate(row) {
  getTank(row.tankId).then((response) => {
    tankForm.value = response.data;
    openTank.value = true;
    title.value = "编辑储罐设备";
  });
}

/** 删除储罐设备 */
function handleTankDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.tankName + '"的数据项?')
    .then(function () {
      return deleteTank(row.tankId);
    })
    .then(() => {
      getTankList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 取消储罐对话框 */
function cancelTank() {
  openTank.value = false;
}

/** 提交储罐表单数据 */
function submitTankForm() {
  proxy.$refs["tankFormRef"].validate((valid) => {
    if (valid) {
      if (tankForm.value.tankId != undefined) {
        updateTank(tankForm.value).then((response) => {
          proxy.$modal.msgSuccess("修改储罐设备成功");
          openTank.value = false;
          getTankList();
        });
      } else {
        addTank(tankForm.value).then((response) => {
          proxy.$modal.msgSuccess("新增储罐设备成功");
          openTank.value = false;
          getTankList();
        });
      }
    }
  });
}

/** 重置 */
function reset() {
  form.value = {
    tankId: checkTank,
  };
  proxy.resetForm("formRef");
}

/** 处理新增监测位号 */
function handleAdd() {
  open.value = true;
  title.value = "新增储罐监测点";
  reset();
}

/** 处理删除 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.monitoringName + '"的数据项?')
    .then(function () {
      return deleteMonitor(row.monitoringId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 处理编辑 */
function handleUpdate(row) {
  getMonitor(row.monitoringId).then((response) => {
    open.value = true;
    title.value = "编辑监测点数据";
    form.value = response.data;
  });
}
/** 处理取消 */
function cancel() {
  open.value = false;
}

/** 处理数据提交 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.monitoringId != undefined) {
        updateMonitor(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMonitor(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 获取监测点数据报警规则数据列表 */
function getRuleList() {
  getMonitorRule().then((res) => {
    rulesOptions.value = res.data;
    console.log(res);
  });
}
getTankList();
getList();
getDeptTree();
getRuleList();
</script>
